// xc/components/diy/seachinput/seachinput.js
var actmember = require('../../../model/member')
var acttoolv2 = require('../../../sys/toolv2')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    memberinfo: {
      type: Object,
      default: ''
    },
    diypara: {
      type: Object,
      default: ''
    },
    objmodel: {
      type: Object,
      default: ''
    },
    groupid: {
      type: String,
      default: ''
    },
    diyid: {
      type: String,
      default: ''

    }
  },

  //  lifetimes(){

  //  },

  ready() {
   
  },

  /**
   * 组件的初始数据
   */
  data: {
    memberid: "",
    clubinfo: null,
    useraddr: null,
    clubdelivery: "1",
    delivery_config: {
      id: 1
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      console.log("init")
      var app = getApp()
      var nowPage = getCurrentPages();
      var actpage = null
      var pagelen = nowPage.length;
      var updatemodel = {};
      if (pagelen > 0) {
        actpage = nowPage[pagelen - 1]
        if (actpage.options) {
          if (actpage.options.diyid) {
            updatemodel["diyid"] = actpage.options.diyid
          }
          if (actpage.options.clubid) {
            clubid = actpage.options.clubid
          }
        }
      }
      var config = app.globalData.configdata
      if (config.prothem && config.prothem.themcolor) {
        updatemodel["themconfig"] = config.prothem.themcolor.config;
      }
      updatemodel["delivery_config"] = app.globalData.configdata.delivery_config;
      console.log("----------------updatemodel-------------")
      console.log(updatemodel)

      this.setData(updatemodel)
      this.getmycloubinfo(this.data.diypara.clubid)
    },
    getaddrinfo() {
      var reslut = {};
      reslut = {
        clubinfo: null,
        useraddr: null
      }
      reslut["clubinfo"] = this.data.clubinfo;
      reslut["clubdelivery"] = this.data.clubdelivery;
  

      if (reslut["clubdelivery"] == 2) {
        reslut["useraddr"] = this.data.useraddr;
      }
      return reslut;
    },
    selectaddrfunc: function (ev) {

      var detail = ev.detail
      this.setData({
        clubinfo: detail.clubinfo,
        useraddr: detail.selectaddr,
        clubdelivery: "2"
      })
      this.triggerEvent("getClubInfo",detail.clubinfo)
    },
    selectuseraddr() {
      var clubinfo = this.data.clubinfo

      var address = this.selectComponent("#address");
      address.set_init(clubinfo, this.data.useraddr);
    },
    clubdeliverychang: function (ev) {
      if (ev.detail == 2) {
        var clubinfo = this.data.clubinfo
        var address = this.selectComponent("#address");
        address.set_init(clubinfo, this.data.useraddr);
        //配送的
      } else {
        this.setData({
          clubdelivery: "1"
        })
      }
    },
    setgroupaddr: function (ev) {


      var actid = -1;
      if (this.data.clubinfo) {
        actid = this.data.clubinfo.id
      }
      var useraddr = null;

      var dataset = ev.currentTarget.dataset
      if (dataset.type != "noaddr") {
        useraddr = this.data.useraddr
      }

      console.log(actid)

      console.log(useraddr)

      var clubaddress = this.selectComponent("#clubaddress")
      clubaddress.showselect(actid, useraddr)

    },
    sharegroup: function () {
      var widgetsshare = this.selectComponent("#widgetsshare");
      widgetsshare.showSharefunc()
    },
    selectgroupaddr: function (ev) {
      var app = new getApp()
      app.globalData.clubinfo = ev.detail;
      actmember.setclub(ev.detail.id).then(res => {

      })
      this.setData({
        clubinfo: ev.detail
      })
      this.triggerEvent("getClubInfo",ev.detail)
    },
    //mark: xx
    getmycloubinfo(clubid) {
      var app = getApp();
      console.log("app.globalData")
      console.log(app.globalData)

      var that=this;
     

      if (app.globalData.clubinfo && !clubid) {
        this.setData({
          clubinfo: app.globalData.clubinfo
        })
        this.triggerEvent("getClubInfo",app.globalData.clubinfo)
      } else {
        actmember.getmycloubinfo(clubid,app.globalData.lng,app.globalData.lat,true).then(res => {
      
          if (res["status"] == 1) {
            var clubinfo = res["obj"]
            if (app.globalData.lng) {
              var preslut = acttoolv2.distanceByLnglat(app.globalData.lng, app.globalData.lat, clubinfo.lng, clubinfo.lat)
              clubinfo["juli"] = preslut["formatval"] + preslut["unit"];
            }
           
            this.setData({
              clubinfo: clubinfo
            })
            this.triggerEvent("getClubInfo",clubinfo)

          }else{
            // var clubaddress = that.selectComponent("#clubaddress")
            // clubaddress.showselect()
          }
        })
      }

    },
  }
})